#include "MemoryDBTest.h"

#include "helper/sqlite/MemoryDB.h"
#include "helper/sqlite/Statement.h"
#include "helper/sqlite/ActionResult.h"

namespace test {
using IDB = helper::sqlite::IDB;
using MemoryDB = helper::sqlite::MemoryDB;

IDB * EmptyMemoryDBTest::s_db = nullptr;

void EmptyMemoryDBTest::SetUpTestCase() {
  //std::cout << "EmptyMemoryDBTest:SetUpTestCase" << std::endl;
  s_db = new MemoryDB();
  s_db->open();
}

void EmptyMemoryDBTest::TearDownTestCase(){
  s_db->close();
  delete s_db;
  s_db = nullptr;
}

EmptyMemoryDBTest::EmptyMemoryDBTest()
:_db(*s_db)
{
}
EmptyMemoryDBTest::~EmptyMemoryDBTest(){
}






void MemoryDBTest::createTable(IDB & _db){
  std::error_code ec;
  auto && stmt = _db.createStatement("create table tab1 (one text primary key, two int);", ec);
  ASSERT_TRUE(stmt && !ec);

  auto && r = stmt->action(); //ActionResult 
  ASSERT_TRUE(r) << "stmt execute error:" << r.errorCode() << std::endl;
}

void MemoryDBTest::dropTable(IDB & _db){
  std::error_code ec;
  auto && stmt = _db.createStatement("drop table tab1", ec);
  ASSERT_TRUE(stmt && !ec);

  auto && r = stmt->action(); //ActionResult 
  ASSERT_TRUE(r) << "stmt execute error:" << r.errorCode() << std::endl;
}

void MemoryDBTest::SetUpTestCase(){
  EmptyMemoryDBTest::SetUpTestCase();
  //std::cout << "MemoryDBTest::SetUpTestCase" << std::endl;

  createTable(*EmptyMemoryDBTest::s_db);
}

void MemoryDBTest:: TearDownTestCase(){
  //std::cout << "MemoryDBTest::TearDownTestCase" << std::endl;
  dropTable(*EmptyMemoryDBTest::s_db);

  EmptyMemoryDBTest::TearDownTestCase();
}

MemoryDBTest::MemoryDBTest()
{
}
MemoryDBTest::~MemoryDBTest() {
}

void MemoryDBTest::SetUp() {
  std::error_code ec;
  auto && stmt = db().createStatement("delete from  tab1", ec);
  ASSERT_TRUE(stmt && !ec);

  auto && r = stmt->execute(); //ExecutionResult 
  ASSERT_TRUE(r) << "stmt execute error:" << r.errorCode() << std::endl;
}

void MemoryDBTest::TearDown()  {
}

}//namespace test
